package tutorial.lite_mod.comment_sth;


import com.example.SpecialApplicationAfter4Example;
import com.example.SpecialApplicationBefore4Example;
import com.wholefarm.model.SpecailApplicationInterface;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.log4j.Logger;
import public_util.database.ConnectionFactory;
import public_util.model.*;
import public_util.servlet.ServletParent;
import public_util.tool_4_handle.Tool_4_Servlet;
import system_api_4_trade.DuzikaiSpecialApplicationFactory;
import system_api_4_trade.SpecialApplicationAfter;
import system_api_4_trade.SpecialApplicationBefore;
import system_api_4_trade.dao.ApiPublicMiddleTableMapper;
import system_api_4_trade.util.tool_4_handle.DateToolApi;
import system_api_4_trade.util.tool_4_handle.Tool_4_ServletApi;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


class Tutorial4LiteModTool {

    private static Logger logger = Logger.getLogger(Tutorial4LiteModTool.class);





    /**
     *  修改排班数据
     */
    public static ReturnData batch_add_comments_4_goods(SqlSessionFactory sqlSession, ServletParent thisParent, HttpSession hsession, HttpServletResponse response, HttpServletRequest request){

        logger.info("----batch_add_store_info----");
        ReturnData resultData = new ReturnData();
        PublicDataAdminUser adminUser = (PublicDataAdminUser) Tool_4_ServletApi.getAttribute(null, hsession, Const4PublicDataBase.MEM_4_self_public_data_admin_user);
        PublicAgentInfo agentInfo = (PublicAgentInfo)Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_agent_info);
        PublicUserInfo userInfo = (PublicUserInfo) Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_user_info);

        if (userInfo == null)
        {
            resultData.setMsg("用户未登录");
            return resultData;
        }
        resultData = Tool_4_ServletApi.checkAccessType(hsession, resultData, 102);
        if (resultData.getStatus() != 0)
        {
            return resultData;
        }
        String comments_info_data = request.getParameter("comments_info_data");
        String id_public_goods_order_record = request.getParameter("id_public_goods_order_record");
        if (comments_info_data == null)
        {
            resultData.setMsg("comments_info_data-数据未传入");
            return resultData;
        }

        if (id_public_goods_order_record == null)
        {
            resultData.setMsg("id_public_goods_order_record-数据未传入");
            return resultData;
        }

        SqlSession session = sqlSession.openSession();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Transaction newTransaction = transactionFactory.newTransaction(session.getConnection(), false);
        Tutorial4LiteModMapper liteModMapper = session.getMapper(Tutorial4LiteModMapper.class);
        try {
            List<PublicMiddleTable> list4MiddleTable = new ArrayList<>();
            loadBatchMiddleList(comments_info_data, agentInfo, userInfo, list4MiddleTable, id_public_goods_order_record);
            liteModMapper.bachInsert2table(list4MiddleTable);
            session.commit();
            resultData.setStatus(0);
            return resultData;
        }
        catch(Exception e)
        {
            try {
                newTransaction.rollback();
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;

            } catch (SQLException e1) {
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;
            }
        }
        finally
        {
            session.close();
        }
    }



    private static void loadBatchMiddleList(String comments_info_data, PublicAgentInfo agentInfo, PublicUserInfo userInfo, List<PublicMiddleTable> list4MiddleTable, String id_public_goods_order_record)
    {
        String commentsArr[] = comments_info_data.split("_-_");
        Date process_day = new Date();
        for ( int i = 0; i < commentsArr.length; i++)
        {
            String signle_str_4_goods = commentsArr[i];
            String arrys[] = signle_str_4_goods.split("-_-");
            if (arrys.length != 8)
            {
                continue;
            }

            String id_goods_str = arrys[0];
            String goods_name_str = arrys[1];
            String id_agent_str = arrys[2];
            String agent_name_str = arrys[3];
            String comments_content_str = arrys[4];
            String commmenst_pic_str = arrys[5];
            String commenst_star_str = arrys[6];
            String comments_type = arrys[7];
            try
            {
                int id_goods = -1;
                try
                {
                    id_goods = Integer.valueOf(id_goods_str);
                }catch (Exception e)
                {
                    continue;
                }
                PublicMiddleTable item4Middle = new PublicMiddleTable();
                item4Middle.setType_middle_app(72001);
                item4Middle.setType_inner_app(1001);
                item4Middle.setId_public_agent_info(userInfo.getId_public_agent_info());
                item4Middle.setId_public_user_info(userInfo.getId_public_user_info());
                item4Middle.setId_public_goods_info(id_goods);
                item4Middle.setIs_online("0");
                item4Middle.setMid_table_remark_001(agentInfo.getAgent_name());
                item4Middle.setMid_table_remark_002(userInfo.getNick_name());
                item4Middle.setMid_table_remark_003(userInfo.getWeichat_img());
                item4Middle.setMid_table_remark_004(goods_name_str);
                item4Middle.setMid_table_remark_005(id_agent_str);
                item4Middle.setMid_table_remark_006(agent_name_str);

                item4Middle.setMid_table_remark_007(comments_content_str);
                item4Middle.setMid_table_remark_008(commmenst_pic_str);
                item4Middle.setMid_table_remark_009(commenst_star_str);
                item4Middle.setMid_table_remark_010(comments_type);
                item4Middle.setMid_table_remark_014(id_public_goods_order_record);


                //商品id1-_-商品名称-_-供应商ID-_-供应商名称-_-评价内容-_-评价图片-_-评分-_-是否匿名
                item4Middle.setMid_table_remark_040(DateToolApi.format(process_day, "yyyyMMdd"));//年月日
                item4Middle.setMid_table_remark_041(DateToolApi.format(process_day, "yyyyMM"));//年月
                item4Middle.setMid_table_remark_042(DateToolApi.format(process_day, "yyyy"));//年
                item4Middle.setTime_insert(new Date());
                item4Middle.setTime_update(new Date());
                item4Middle.setTime_4_begin(process_day);
                item4Middle.setId_public_data_admin_user(agentInfo.getId_public_data_admin_user());
                list4MiddleTable.add(item4Middle);
            }catch(Exception e)
            {
                logger.info(signle_str_4_goods + "====error--" + e);
                continue;
            }
        }
    }



    public static void main(String[] args) throws Exception {

        /**
         * 验证supply的批量更新, 后面像加入购物车的操作,就会变得比较简单
         */
        SpecialApplicationBefore beforeApp = new SpecialApplicationBefore4Example();
        //本地环境
        beforeApp.setConfiguartion_environment(new String[]{SpecailApplicationInterface.LOAD_CONF_TYPE_FROM_DB, "1021"});
        /*按照独自开重构思路, 进行相关的特殊处理的方法*/
        SpecialApplicationAfter afterApp = new SpecialApplicationAfter4Example();
        DuzikaiSpecialApplicationFactory.setInitApplication(beforeApp, afterApp);
        SqlSessionFactory sqlSession = ConnectionFactory.getSession();
        SqlSession session = sqlSession.openSession();
        try
        {
            Tutorial4LiteModMapper t4lMapper = session.getMapper(Tutorial4LiteModMapper.class);
            List<PublicGoodsInfo> publicGoodsInfos = new ArrayList<>();

            PublicGoodsInfo g1 = new PublicGoodsInfo();
            g1.setId_public_goods_info(160);
            g1.setSupply_left_count(1600);
            g1.setId_public_agent_info(300098);

            PublicGoodsInfo g2 = new PublicGoodsInfo();
            g2.setId_public_goods_info(165);
            g2.setSupply_left_count(16500);
            g2.setId_public_agent_info(300098);
            publicGoodsInfos.add(g1);
            publicGoodsInfos.add(g2);
            session.commit();
        }
        catch(Exception e)
        {
            Tool_4_Servlet.showStackTrace(e);
            logger.info(e);
        }
        finally
        {
            session.close();
        }
    }

}